/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//import java.util.*;
import java.io.*;
import java.util.Scanner;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Reads a matrix out of a file and creates a double[][] object out of it.
 *
 * @author Corvus
 */
public class MatrixReader {

    double[][] matrix;

    /**
     * Reads an .xml file
     * 
     * @param fileName
     * @return The matrix as a double[][] object
     * @throws SAXException
     * @throws IOException
     */
    public double[][] readXML(final String fileName) throws SAXException, IOException {
        SAXParser saxParser;
        SAXParserFactory factory;

        try {
            factory = SAXParserFactory.newInstance();
            saxParser = factory.newSAXParser();
        } catch (ParserConfigurationException ex) {
            throw new Error();
        } catch (SAXException ex) {
            throw ex;
        }

        DefaultHandler handler = new DefaultHandler() {

            int rows = -1;
            int columns = -1;
            int currentRow;
            int currentValue;

            @Override
            public void startElement(String uri, String localName,
                    String qName, Attributes attributes) throws SAXException {


                if (qName.equalsIgnoreCase("matrix")) {
                    if (attributes.getIndex("rows") == -1) {
                        throw new SAXException("Missing Attribute: rows @ " + fileName);
                    }
                    if (attributes.getIndex("columns") == -1) {
                        throw new SAXException("Missing Attribute: columns @ " + fileName);
                    }
                    rows = Integer.parseInt(attributes.getValue("rows"));
                    columns = Integer.parseInt(attributes.getValue("columns"));
                    currentRow = 0;
                    matrix = new double[rows][columns];
                } else if (qName.equalsIgnoreCase("row")) {
                    currentValue = 0;
                } else if (qName.equalsIgnoreCase("value")) {
                }

            }

            @Override
            public void characters(char ch[], int start, int length) {
                matrix[currentRow][currentValue] = Double.parseDouble(new String(ch, start, length));
            }

            @Override
            public void endElement(String uri, String localName,
                    String qName) {
                if (qName.equalsIgnoreCase("row")) {
                    currentRow = currentRow + 1;
                } else if (qName.equalsIgnoreCase("value")) {
                    currentValue = currentValue + 1;
                }

            }
        };

        saxParser.parse(fileName, handler);
        return matrix;
    }

    /**
     * Reads a binary file
     * 
     * @return The matrix as an double[][] object
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static double[][] readBinary(String filename) throws FileNotFoundException, IOException {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
        double[][] matrix = null;
        try {
            matrix = (double[][]) in.readObject();
        } catch (ClassNotFoundException ex) {
        }
        in.close();

        return matrix;
    }

    public static double[][] readKerola(final String filename) throws FileNotFoundException {
        Scanner in = new Scanner(new File(filename));
        int n = in.nextInt();

        double[][] matrix = new double[n][n];
        for (int row = 0; row < n; row++) {
            for (int col = 0; col < n; col++) {
                matrix[row][col] = Double.parseDouble(in.next());
            }
        }
        return matrix;
    }

    /**
     * Quick test for readKerola
     * @param args
     * @throws FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException {
        double[][] matriisi = null;

        matriisi = readKerola("matr1_7500.txt");
        if (matriisi.length == 7500) {
        	System.out.println("Oikea koko");
        }

    }
}
